package com.springboot.demo.web;
import com.springboot.demo.entity.Book;
import com.springboot.demo.service.SearchBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
@Controller
public class SearchBookController {
    @Autowired
    private SearchBookService searchbookService;
    @RequestMapping("/reader_searchbook(in)")
    public String reader_searchbook(){
        return "reader_searchbook(in)";
    }
    @RequestMapping("/reader_searchbook(no)")
    public String reader_searchbook_no(){
        return "reader_searchbook(no)";
    }
    @RequestMapping("/reader_search_result_in")
    public String reader_search_result_in(){
        return "reader_search_result(in)";
    }
    @RequestMapping("/reader_search_result_no")
    public String reader_search_result_no(){
        return "reader_search_result(no)";
    }
    @RequestMapping("/reader_bookinfo")
    public String reader_bookinfo(){
        return "reader_bookinfo";
    }
    //模糊查询获取书籍信息并去重显示
    @RequestMapping(value = "/reader_search",method = RequestMethod.POST)
    @ResponseBody
    public List<Book> reader_search(String bname) throws UnsupportedEncodingException {
        List<Book> list = new ArrayList<>();
        List<Book> list1 = new ArrayList<>();
        list.addAll(searchbookService.getBook(bname));
        list1.addAll(list);
        for (int i = 0; i < list1.size() - 1; i++) {
            for (int j = list1.size() - 1; j > i; j--) {
                if (list1.get(j).getBname().equals(list1.get(i).getBname())&&list1.get(j).getAuthor().equals(list1.get(i).getAuthor())) {
                    list1.remove(j);
                }
            }
        }

        return list1;
    }
    //根据书名与作者确切查询获取书籍信息并全部显示
    @RequestMapping(value = "/reader_search1",method = RequestMethod.POST)
    @ResponseBody
    public List<Book> reader_search1(String bname,String author) throws UnsupportedEncodingException {
        List<Book> list = new ArrayList<>();
        list.addAll(searchbookService.getBook1(bname,author));
        return list;
    }

}